package com.yc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.bean.Coupon;
import com.yc.bean.FoodBean;
import com.yc.bean.OrderBean;
import com.yc.bean.OrderDetailBean;
import com.yc.mapper.OrderMapper;
import com.yc.model.FoodVO;
import com.yc.model.OrderListVO;
import com.yc.model.ResponseResult;
import com.yc.service.FoodBiz;
import com.yc.service.OrderBiz;
import com.yc.service.OrderDetailBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zwei
 * @create 2024-10-08 16:16
 */
@RestController
@RequestMapping("cart")
public class ShowOrderController {
    private OrderBiz orderBiz;
    private OrderDetailBiz orderDetailBiz;
    private OrderMapper orderMapper;
    private FoodBiz foodBiz;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    public void setOrderBiz(OrderBiz orderBiz, OrderDetailBiz orderDetailBiz, OrderMapper orderMapper, FoodBiz foodBiz) {
        this.orderBiz = orderBiz;
        this.orderDetailBiz = orderDetailBiz;
        this.orderMapper = orderMapper;
        this.foodBiz=foodBiz;
    }

    @PostMapping("payfor/{order_code}")
    public ResponseResult payfor(@PathVariable String order_code) {
        Boolean i= orderBiz.update(order_code);
        if (i){
            return ResponseResult.ok();
        }else {
            return ResponseResult.error();
        }
    }
    @PostMapping("delectorder/{id}")
    public ResponseResult delectorder(@PathVariable int id) {
        Boolean i= orderBiz.delete(id);
        if (i){
            //修改优惠卷状态和过期时间
            Coupon coupon= (Coupon) redisTemplate.opsForValue().get(17+"");
            if (coupon!=null){
                coupon.setCid(1);
                // 将更新后的 Coupon 对象重新存入 Redis
                // 先获取原来的过期时间
                Long expiration=redisTemplate.getExpire(17+"");
                // 更新 Coupon 对象
                redisTemplate.opsForValue().set(17+"", coupon);
                // 设置相同的过期时间
                if (expiration != null) {
                    redisTemplate.expire(17+"", expiration, TimeUnit.SECONDS);
                }
            }
            return ResponseResult.ok();
        }else {
            return ResponseResult.error();
        }
    }
    @PostMapping("ordered/{userid}")
    public ResponseResult order(@PathVariable int userid) {
        List<OrderBean> list = orderBiz.find(userid);
        return ResponseResult.ok().setData(list);
    }
    @GetMapping("orderDetail")
    public ResponseResult orderDetail() {
        List<OrderDetailBean> list = orderDetailBiz.find();
        return ResponseResult.ok().setData(list);
    }
    @PostMapping("orderList")
    public ResponseResult orderList(@RequestBody OrderListVO orderListVO) {
        //修改菜品购买数量
        List<FoodVO> foods=orderListVO.getFoodVO();
        for (FoodVO foodVO:foods){
            foodBiz.updateFood(foodVO.getId(),foodVO.getCount());
        }
        // 生成订单编号
        double totalPriceDouble = orderListVO.getTotalPrice();
        int totalPriceInt = (int) totalPriceDouble; // 将浮点数转换为整数
        LocalDateTime now = LocalDateTime.now();
        String orderDateTime = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        if(orderListVO.getDinnerTableId()==null){
            orderListVO.setDinnerTableId(10);
        }
        String orderCode = orderDateTime + orderListVO.getDinnerTableId() + totalPriceInt;
        int result=orderBiz.setOrderList(orderListVO.getUserid(),orderCode,orderListVO.getDinnerTableId(),orderListVO.getTotalPrice());
        QueryWrapper qw=new QueryWrapper();
        qw.eq("order_code",orderCode);
        List<OrderBean>order=orderMapper.selectList(qw);
        int id=order.get(0).getId();
        if (result<0){
            return ResponseResult.error();
        }
        List<FoodVO>list=orderListVO.getFoodVO();
        for (FoodVO foodVO : list){
            Boolean i=orderDetailBiz.setOrderDetail(id,foodVO.getId(),foodVO.getCount(),foodVO.getDiscount());
        }
        return ResponseResult.ok();
    }
}
